Function Create-InstallJob
{
	[CmdletBinding()]
	Param
	(
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$ClusterName,
		
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$AdminLogin,
		
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$AdminPassword,
		
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$CertificateThumbprint,
		
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$StorageAccountName,
		
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$StorageAccountKey,
		
		[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
		[string]$InstallContainer
	)

	# Fill in hpc scheduler credentials
	$hpcCredentials = New-Object CloudBlitz.Powershell.Cmdlets.HpcSchedulerCredentials
	$hpcCredentials.ClusterName = $ClusterName
	$hpcCredentials.AdminLogin = $AdminLogin
	$hpcCredentials.AdminPassword = $AdminPassword
	$hpcCredentials.CertificateThumbprint = $CertificateThumbprint

	# Create the install job on the scheduler
	$installJob = New-Object HpcSchedulerManagement.DataContracts.SchedulerJob
    $installJob.FailOnTaskFailure = $True
	$installJob.Name = "Install job"
	$installJob.Id = Add-SchedulerJob -Credentials $hpcCredentials -Job $installJob -ErrorAction Stop

	Write-Host "Install job created with id = $($installJob.Id)"

	# Create install directory
	$createDirTask = New-Object HpcSchedulerManagement.DataContracts.SchedulerTask
	$createDirTask.Name = "Create directory"
	$createDirTask.CommandLine = "clusrun /nodes:compute* md C:\Install"
	$createDirTask.Id = Add-SchedulerTask -Credentials $hpcCredentials -JobId $installJob.Id -Task $createDirTask
	Write-Host "Create dir task created with id = $($createDirTask.Id)"
	
	Write-Host "Enumerating blobs in the install container '$installContainer'"
	$blobs = hpcpack list /account:$StorageAccountName /key:$StorageAccountKey /container:$InstallContainer
	$dependsOn = ""
    for($i = 2; $i -lt $blobs.Length - 1; $i++)
    {
        $blobName = $blobs[$i].Split("  ", [System.StringSplitOptions]::RemoveEmptyEntries)[3]

	    Write-Host "Creating download task for '$blobName' blob"
	    $downloadBinaryTask = New-Object HpcSchedulerManagement.DataContracts.SchedulerTask
	    $downloadBinaryTask.Name = "Download $blobName"
	    $downloadBinaryTask.CommandLine = "clusrun /nodes:computenode* hpcpack download $blobName /account:$StorageAccountName /key:$StorageAccountKey /container:$InstallContainer /path:C:\Install"
	    $downloadBinaryTask.DependsOn = $createDirTask.Name
	    $downloadBinaryTask.Id = Add-SchedulerTask -Credentials $hpcCredentials -JobId $installJob.Id -Task $downloadBinaryTask

	    $dependsOn += ",$($downloadBinaryTask.Name)"

	    Write-Host "Download task for $blobName created with id = $($downloadBinaryTask.Id)"
    }

	# Add the final task that runs the start up script
	$runInstallScriptTask = New-Object HpcSchedulerManagement.DataContracts.SchedulerTask
	$runInstallScriptTask.Name = "Run install script"
	$runInstallScriptTask.CommandLine = "clusrun /nodes:compute* C:\Install\startup.cmd"
	$runInstallScriptTask.DependsOn = $dependsOn
	$runInstallScriptTask.Id =  Add-SchedulerTask -Credentials $hpcCredentials -JobId $installJob.Id -Task $runInstallScriptTask
	Write-Host "Run install script task created with id = $($runInstallScriptTask.Id)"

	# Submit the install job
	Write-Host "Submitting the install job"
	Submit-SchedulerJob -Credentials $hpcCredentials -JobId $installJob.Id

	# Wait for the job to complete
	Write-Host "Waiting for job $($installJob.Id) to complete"
	$finalState = Wait-SchedulerJob -Credentials $hpcCredentials -JobId $installJob.Id

	Write-Host "Install job $finalState"
	
	return $finalState
}